package chpt10;
import java.awt.BorderLayout;
import java.awt.CardLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class CardLayoutTest {
    public static void main(String[] args) {
        JFrame frame = new JFrame("CardLayout Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        // 放到窗体中央区域的面板（将在其中添加3个面板）
        JPanel panel = new JPanel(new CardLayout());
        // panel1设置为BorderLayout，为了让文本域填满区域
        JPanel panel1 = new JPanel(new BorderLayout());
        panel1.setBorder(new TitledBorder("面板1"));
        panel1.add(new JTextArea("这是一个文本区域"));

        JPanel panel2 = new JPanel();
        panel2.setBorder(new TitledBorder("面板2"));
        panel2.add(new JButton("按钮"));

        JPanel panel3 = new JPanel();
        panel3.setBorder(new TitledBorder("面板3"));
        panel3.add(new JTextField(10));

        panel.add(panel1, "面板1");
        panel.add(panel2, "面板2");
        panel.add(panel3, "面板3");

        JButton nextButton = new JButton("下一个");
        nextButton.addActionListener(e -> {
            CardLayout cardLayout = (CardLayout) panel.getLayout();
            cardLayout.next(panel); // 切换到下一个面板
        });

        JButton panel2Button = new JButton("面板2");
        panel2Button.addActionListener(e -> {
            CardLayout cardLayout = (CardLayout) panel.getLayout();
            cardLayout.show(panel, "面板2"); // 切换到指定名称面板
        });

        JButton lastPanelButton = new JButton("最后一个面板");
        lastPanelButton.addActionListener(e -> {
            CardLayout cardLayout = (CardLayout) panel.getLayout();
            cardLayout.last(panel); // 切换到最后一个面板
        });

        JPanel southPanel = new JPanel();
        southPanel.add(nextButton);
        southPanel.add(panel2Button);
        southPanel.add(lastPanelButton);
        frame.add(panel, BorderLayout.CENTER);
        frame.add(southPanel, BorderLayout.SOUTH);
        frame.setVisible(true);
    }
}